import requests
from lxml import etree
import threading
from queue import  Queue
import json
from urllib import request


# def getinfo(url):
#
    # response = requests.get(url,headers=headers)
    # text = response.text
    # # print(text)
    # html = etree.HTML(text)
    # res = html.xpath('//h2/text()')
    # for i in res:
    #     print(i)
    # tat = len(threading.enumerate())
    # print('上课拉萨的卡拉的%s' % tat)
# # print(res)

class MyThread(threading.Thread):
    def __init__(self,t_q,d_q,headers):
        super(MyThread,self).__init__()
        self.t_q = t_q
        self.d_q = d_q
        self.headers = headers
    def run(self):
        while not self.t_q.empty():
            base_url = self.t_q.get()
            response = requests.get(base_url, headers=self.headers)
            if 200 <= response.status_code <300:
                self.d_q.put(response.text)
            else:
                print('%s请求失败'%base_url)
        # for i in range(1, 5):
        #     url_q = url % i
        #     # print(url_q)
        #     t1 = threading.Thread(target=getinfo, args=(url_q,))
        #     t1.start()

class MyThread2(threading.Thread):
    def __init__(self,d_q,get_list):
        super(MyThread2,self).__init__()
        self.d_q = d_q
        self.get_list = get_list
        self.flag = True
        self.f = f
    def run(self):
        while True:
            if i.is_alive() or not self.d_q.empty():
                # 读取源代码
                try:
                    html = self.d_q.get(timeout=3)
                    self._parse(html)
                except:
                    html = None
            else:

                self.flag = False
                break

        def getinfo(url):

            response = requests.get(url,headers=headers)
            text = response.text
            # print(text)
            html = etree.HTML(text)
            res = html.xpath('//h2/text()')
            for i in res:
                print(i)
            tat = len(threading.enumerate())
            print('上课拉萨的卡拉的%s' % tat)
        # print(res)




if __name__ == '__main__':

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3702.0 Safari/537.36"

    }

    t_q = Queue()
    d_q = Queue()
    url = "https://www.qiushibaike.com/hot/page/%s/"

    url_q = (url % i for i in range (1,14))
        # print(url_q)
    for i in url_q:
        t_q.put(i)
        # t1 = threading.Thread(target=getinfo,args=(url_q,))
        # t1.start()
    threading_list = []
    for i in range(0,8):
        t = MyThread(t_q,d_q,headers)
        t.start()
        threading_list.append(t)

    par_list = []
    for i in range(0,8):
        t = MyThread2(d_q,url_q)
        t.start()
        par_list.append(t)

    for i in threading_list:
        i.join()
    for i in par_list:
            i.join()



